package com.kiliter.handler;

import cn.dev33.satoken.exception.NotLoginException;
import com.baomidou.mybatisplus.extension.api.R;
import com.kiliter.exception.KiliterException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName zhangjl
 * @date 2021/6/1 14:21
 * @Version 1.0
 */
@RestControllerAdvice
@AllArgsConstructor
@Slf4j
public class kiliterExceptionHandler {


    @ExceptionHandler(value = KiliterException.class)
        public R handlerOrderException(KiliterException e){
        System.out.println(e.getMessage());
        return R.failed(e.getMessage()).setCode(e.getCode());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e){

        String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return R.failed(message);
    }

    @ExceptionHandler(NotLoginException.class)
    public R handleNotLoginException(NotLoginException e){

        String message = e.getMessage();
        return R.failed(message);
    }

    @ExceptionHandler(ClientAbortException.class)
    public void handleIOException(ClientAbortException e,HttpServletResponse resp){
        log.info("用户取消下载");
    }
}
